package com.pirestupppai.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.bean.LoginUser;
import com.pirestupppai.common.Constant;
import com.pirestupppai.common.Urls;
import com.pirestupppai.update.DownloadService;
import com.pirestupppai.update.UpdateManager;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.utils.Utils;
import com.pirestupppai.widgets.dialog.ConfirmDialog;

import butterknife.BindView;

/**
 * 设置页
 *
 * @author: xiaoluo
 * @date: 2017-03-16 17:03
 */
public class SettingActivity extends BaseActivity {

    @BindView(R.id.account_exit_tv)
    TextView exitTv;
    @BindView(R.id.account_safe_tv)
    TextView accountSafeTv;
    @BindView(R.id.edit_info_tv)
    TextView editInfoTv;
    @BindView(R.id.notification_tv)
    TextView notificationTv;
    @BindView(R.id.update_tv)
    TextView updateBtn;
    @BindView(R.id.clear_tv)
    TextView clearTv;
    @BindView(R.id.rating_tv)
    TextView ratingTv;
    @BindView(R.id.feedback_tv)
    TextView feedbackTv;
    @BindView(R.id.about_btn)
    TextView aboutTv;

    private UpdateManager mUpdateManager;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {
        exitTv.setOnClickListener(this);
        accountSafeTv.setOnClickListener(this);
        editInfoTv.setOnClickListener(this);
        notificationTv.setOnClickListener(this);
        updateBtn.setOnClickListener(this);
        clearTv.setOnClickListener(this);
        ratingTv.setOnClickListener(this);
        feedbackTv.setOnClickListener(this);
        aboutTv.setOnClickListener(this);
    }

    @Override
    protected void initLogic() {
        mUpdateManager = new UpdateManager(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.account_exit_tv:
                ConfirmDialog exitDialog = new ConfirmDialog(mContext)
                        .setMessage("确定要退出?")
                        .setLeftText("取消")
                        .setRightText("确定")
                        .setOnSelectListener(new ConfirmDialog.OnSelectListener() {
                            @Override
                            public void onLeftSelect() {

                            }

                            @Override
                            public void onRightSelect() {
                                LoginUser.TokenVerity.setToken("");
                                Intent intent = new Intent(mContext, InputPhoneActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                        });
                exitDialog.show();
                break;
            case R.id.account_safe_tv:
                intent.setClass(mContext, AccountSafeActivity.class);
                startActivity(intent);
                break;
            case R.id.edit_info_tv:
                intent.setClass(mContext, EditInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.notification_tv:
                intent.setClass(mContext, NotificationActivity.class);
                startActivity(intent);
                break;
            case R.id.update_tv:
                if (!DownloadService.isDownloading) {
                    mUpdateManager.checkUpdate();
                    mUpdateManager.showToast(true);
                } else {
                    Utils.showToast("已经在后台下载");
                }
                break;
            case R.id.clear_tv:
                ConfirmDialog clearDialog = new ConfirmDialog(mContext)
                        .setMessage("确定要清除掉缓存吗?")
                        .setLeftText("取消")
                        .setRightText("确定")
                        .setOnSelectListener(new ConfirmDialog.OnSelectListener() {
                            @Override
                            public void onLeftSelect() {

                            }

                            @Override
                            public void onRightSelect() {
                                ImageLoadUtil.getInstance().clearMemory(mContext);
                                Utils.showToast("缓存清理成功");
                            }
                        });
                clearDialog.show();
                break;
            case R.id.rating_tv:
                try {
                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                    Intent rateIntent = new Intent(Intent.ACTION_VIEW, uri);
                    rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(rateIntent);
                } catch (Exception e) {
                    Toast.makeText(SettingActivity.this, "打开应用市场失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.feedback_tv:
                intent.setClass(mContext, FeedBackActivity.class);
                startActivity(intent);
                break;
            case R.id.about_btn:
                intent.setClass(mContext, WebActivity.class);
                intent.putExtra(Constant.IntentKey.WEBVIEW_URL, Urls.BASE_URL +
                        "mobile/contacts.html" + "?version=" + Utils.getAppVersion(mContext));
                intent.putExtra(Constant.IntentKey.WEBVIEW_TITLE, "关于我们");
                startActivity(intent);
                break;
        }
    }
}
